package com.sk.tools.excel.meta.model.ser;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.sk.tools.excel.validator.Validator;

import java.io.IOException;

public class ValidatorJsonDeserializer extends JsonDeserializer<Validator> {
    @Override
    public Validator deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
        JsonToken t = jsonParser.getCurrentToken();
        if (t == JsonToken.VALUE_STRING) {
            String text = jsonParser.getText().trim();
            if(text != null && text.length() > 0){
                text = text.substring(0,1).toUpperCase() + text.substring(1);
            }else{

            }
            try {
                try {
                    return (Validator) Class.forName("com.sk.tools.excel.validator.impl." + text + "Validator").newInstance();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

        }
        return null;
    }
}
